package com.heu.blood.common.utils;

import com.heu.blood.common.utils.Constant;
import com.heu.blood.common.vo.SolveXmlVo;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 功能描述：解析XML并返回VO对象，在这里VO可以任意VO，也可以进行自定义修改为泛型
 *
 * @Author: lvyj
 * @Date: 2023/6/23 21:13
 */
public class SolveXmlUtil {
    public List<SolveXmlVo> getSolveResult(String productId) {
        List<SolveXmlVo> resList = null;
        try {
            resList = new ArrayList<>();
            SAXReader reader = new SAXReader();
            // 这里是读取文件的路径，根据windows和Linux不同自由配置
            Document document = reader.read(new File(Constant.WINDOWS_REPORT_ADDR + productId + ".xml"));
            Element element = document.getRootElement();
            // 获取根标签
            Element errors = element.element("标签名");
            for (Iterator iter = errors.elementIterator(); iter.hasNext();){
                Element cur = (Element)iter.next();
                Attribute attribute1 = cur.attribute("子标签名");
                Attribute attribute2 = cur.attribute("子标签名");
                Attribute attribute3 = cur.attribute("子标签名");
                Attribute attribute4 = cur.attribute("子标签名");
                Attribute attribute5 = cur.attribute("子标签名");

                Element location = cur.element("子标签名");

                Attribute attribute6 = location.attribute("子标签名");
                Attribute attribute7 = location.attribute("子标签名");
                // 自由修改
                SolveXmlVo curVo = new SolveXmlVo();
                curVo.setId(attribute1.getValue());
                curVo.setBloodId(attribute2.getValue());
                curVo.setMsg(attribute3.getValue());
                resList.add(curVo);
            }
        }catch (Exception e){
            return resList;
        }
        return resList;
    }
}
